package Controller;

import java.util.ArrayList;

import javax.swing.JPanel;

import DAO.InterfaceDAO.ExerciceDAO;
import Factories.FactoryDAO;
import Model.Exercice;
import Model.Resultat;

public class ExerciceController {

	private Exercice ex;
	private ExerciceDAO exDao;

	public ExerciceController() {
		ex = null;
		this.exDao = FactoryDAO.getInstance(null).createExerciceDAO();
	}

	public ExerciceController(Exercice ex) {
		this.ex = ex;
		this.exDao = FactoryDAO.getInstance(null).createExerciceDAO();
	}

	public ExerciceController(String titreExercice) {
		this.exDao = FactoryDAO.getInstance(null).createExerciceDAO();

		String nameExercice = exDao.nomPlugIn(titreExercice);
		Class<?> c = null;
		try {c = Class.forName(nameExercice);} 
		catch (ClassNotFoundException e) {e.printStackTrace();}

		try {
			ex = (Exercice) c.newInstance();
			ex.setType(titreExercice);
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}

	public JPanel getPanelEtud() {
		return ex.getPanelEtud();
	}

	public JPanel getPanelProf() {
		return ex.getPanelProf();
	}

	public void creer(int auteur) {
		ex.setAuteur(auteur);
		ex.creer();
	}

	public boolean remove() {
		boolean result = true;
		exDao.delete(ex);
		return result;
	}

	/**
	 * Lance le JPanel de l'exercice
	 * @return
	 */
	public JPanel consulter() {
		int id = ex.getId();
		Exercice e = this.exDao.read(id);
		return e.consulter();
	}

	public JPanel consulter(String nom) {
		Exercice e = this.exDao.read(1);
		return e.consulter();
	}

	public boolean editer() {
		boolean result = false;
		ex.editer();
		return result;
	}

	public ResultatController validerReponse(EtudiantController etudiant) {
		ResultatController result = new ResultatController(this.ex, etudiant.getEtudiant());
		return result;
	}

	public void corriger(Resultat res) {
		ex.corriger(res);
	}

	public ArrayList<Exercice> lister() {
		return ex.lister();
	}

	public ArrayList<String> listeTypeExercice() {
		return exDao.listeTypeExercice();
	}

	public ArrayList<String> listeTheme() {
		return exDao.listeTheme();
	}

}
